<?php

namespace app\services;

use app\models\User;
use app\models\Users;
use app\tools\T;
use yii\helpers\Json;


class UsersService extends Services {
    protected $username;
    protected $password;
    protected $mobile;
    static $tokenKey = 'Users:Token';
    protected $limit;
    protected $pageSize;
    protected $page;
    protected $orderBy = 'id DESC';

    /**
     * @return mixed
     */
    public function getUsername() {
        return $this->username;
    }

    /**
     * @param mixed $username
     */
    public function setUsername($username) {
        $this->username = $username;
    }

    /**
     * @return mixed
     */
    public function getPassword() {
        return $this->password;
    }

    /**
     * @param mixed $password
     */
    public function setPassword($password) {
        $this->password = $password;
    }

    /**
     * @return mixed
     */
    public function getMobile() {
        return $this->mobile;
    }

    /**
     * @param mixed $mobile
     */
    public function setMobile($mobile) {
        $this->mobile = $mobile;
    }

    public function getOne() {
        return Users::findOne($this->Params);
    }

    public static function getToken($Users) {
        $tmpArr = [
            'id' => $Users->id,
            'username' => $Users->username,
            'mobile' => $Users->mobile,
            'signature' => \Yii::$app->getSecurity()->generateRandomString(),
        ];

        ksort($tmpArr);
        $tmpStr = T::arrayToString($tmpArr, '=', '&');
        $tokenkey = sha1($tmpStr);
        T::getRedis()->set(self::$tokenKey . ':' . $tokenkey, Json::encode($Users));
        T::getRedis()->expire(self::$tokenKey . ':' . $tokenkey, EXPIRE_TIME * 3600);
        return $tokenkey;
    }

    public function addUsers() {
        $Users = new Users();
        $Users->username = $this->Params['username'];
        $Users->password = md5(sha1($this->Params['password']) . MENG_TOKEN);
        $Users->mobile = $this->Params['mobile'];
        $Users->headimgurl = $this->Params['headimgurl'] ?? HEAD_IMAGE;
        $Users->sex = $this->Params['sex'] ?? 1;
        $Users->ctime = date("Y-m-d H:i:s");
        $Users->utime = date("Y-m-d H:i:s");
        $Users->last_login = date("Y-m-d H:i:s");
        $Users->mark = $this->Params['password'];
        $Users->invite_code = T::createInviteCode();
        $Users->parent_invite_code = $this->Params['parent_invite_code'] ?? '';
        $Users->energy_number = 50;
        return $Users->save();
    }

    /**
     * @return mixed
     */
    public function getLimit() {
        return $this->limit;
    }

    /**
     * @param mixed $limit
     */
    public function setLimit($limit) {
        $this->limit = $limit;
    }

    /**
     * @return int
     */
    public function getPageSize() {
        return $this->pageSize;
    }

    /**
     * @param int $pageSize
     */
    public function setPageSize($pageSize) {
        $this->pageSize = $pageSize;
    }

    /**
     * @return int
     */
    public function getPage() {
        return $this->page;
    }

    /**
     * @param int $page
     */
    public function setPage($page) {
        $this->page = $page;
    }

    /**
     * @return string
     */
    public function getOrderBy() {
        return $this->orderBy;
    }

    /**
     * @param string $orderBy
     */
    public function setOrderBy($orderBy) {
        $this->orderBy = $orderBy;
    }


    public function getList() {
        return Users::find()
            ->where($this->Params)
            ->orderBy($this->orderBy)
            ->offset($this->page)
            ->limit($this->pageSize)
            ->all();
    }

    public function getCount() {
        return Users::find()
            ->where($this->Params)
            ->count();
    }

    public function getAll() {
        return Users::find()
            ->where($this->Params)
            ->all();
    }

    public function onPaySuccess($event) {
        $Users = $event->data['Users'];
        $energy = $event->data['product_energy'];
        $Users->energy_number = $Users->energy_number - $energy;
        if (!$Users->save()) {
            $event->handled = true;
        }
        return $event->handled;
    }
}